package com.own.demo.geodemo;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import lombok.SneakyThrows;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;

import java.util.*;
import java.util.stream.Collectors;

public class ToGeoJSONV3 {
    static int HEIGHT = 100;
    static int AREA = 30;
    static LineBufferImpl defaultBuffer= (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();

    @SneakyThrows
    public static String goGeoJSONV2(List<Geometry> geometries,Geometry geometry) {
        if (geometries == null) {
            return null;
        }
        int row = geometries.size();
        List<Coordinate> allPoints = new ArrayList<>(100000);

        for (int i = 0; i < row; i++) {
            Geometry js = geometries.get(i);
            Coordinate[] coordinates = js.getCoordinates();
            int coordRow = coordinates.length;

            for (int h = 0; h < coordRow; h++) {
                Coordinate coordinate = coordinates[h];
                allPoints.add(coordinate);
            }
        }
        int allPointsLon=allPoints.size();
        System.out.println("点数量："+allPoints.size());

        List<Double> lonList=new ArrayList<>(allPointsLon);
        List<Double> latList=new ArrayList<>(allPointsLon);

        for(int i=0;i<allPointsLon;i++){
            Coordinate coordinate= allPoints.get(i);
            lonList.add(coordinate.x);
            latList.add(coordinate.y);
        }
        System.out.println("x去重前的数量："+lonList.size());
        System.out.println("y去重前的数量："+latList.size());
        lonList = lonList.stream().distinct().collect(Collectors.toList());
        latList = latList.stream().distinct().collect(Collectors.toList());
        System.out.println("x去后前的数量："+lonList.size());
        System.out.println("y去后前的数量："+latList.size());

        int lonListRow=lonList.size();
        Map<String,List<Coordinate>> xMap=new LinkedHashMap(1024);
        for(int i=0;i<lonListRow;i++){
            List<Coordinate> data=new ArrayList<>(1024);
            String tempLon=  lonList.get(i)+"";
            for(int j=0; j<allPointsLon;j++){
                Coordinate coordinate= allPoints.get(j);
                String x=coordinate.x+"";
                if(tempLon.equals(x)){
                    data.add(coordinate);
                }
            }

            data= data.stream().distinct().collect(Collectors.toList());
            data= data.stream().sorted(Comparator.comparing(Coordinate::getY)).collect(Collectors.toList());
            xMap.put(tempLon,data);
        }



        //获取y线
        int latListRow=latList.size();

        Map<String,List<Coordinate>> yMap=new LinkedHashMap(1024);
        for(int i=0;i<latListRow;i++){
            List<Coordinate> dataLat=new ArrayList<>(1024);
            String tempLat=  latList.get(i)+"";
            for(int j=0; j<allPointsLon;j++){
                Coordinate coordinate= allPoints.get(j);
                String y=coordinate.y+"";
                if(tempLat.equals(y)){
                    dataLat.add(coordinate);
                }
            }

            dataLat= dataLat.stream().distinct().collect(Collectors.toList());
            dataLat= dataLat.stream().sorted(Comparator.comparing(Coordinate::getY)).collect(Collectors.toList());
            yMap.put(tempLat,dataLat);
        }


        System.out.println("DELETE from geofence where geofence_id>34669 and service_id=713;");
        for(Map.Entry<String,List<Coordinate>> v:xMap.entrySet()){
            List<Coordinate> value=v.getValue();
            getSql(value);
        }

        System.out.println("-- y线");
        for(Map.Entry<String,List<Coordinate>> v:yMap.entrySet()){
            List<Coordinate> value=v.getValue();
            getSql(value);
        }

        List<List<Coordinate>> allXYdata=new ArrayList<>(1026);
        List<Coordinate> listAll = Arrays.asList(geometry.getCoordinates());

        getSql(listAll);
        allXYdata.add(listAll);
        allXYdata.addAll(xMap.values());
        allXYdata.addAll(yMap.values());
        return dataDraw(allXYdata);
    }

    public static String dataDraw(List<List<Coordinate>> list){
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        int row=list.size();
        for(int j=0;j<AREA;j++) {
            for (int i = 0; i < row; i++) {
                JSONObject geo = new JSONObject(4);
                geo.put("type", "LineString");
                List<Coordinate> temp = list.get(i);
                Coordinate[] coordinates = temp.toArray(new Coordinate[0]);
                int coordRow = coordinates.length;
                List<double[]> gpsList = new ArrayList<>(coordRow);

                for (int h = 0; h < coordRow; h++) {
                    Coordinate coordinate = coordinates[h];
                    double[] gps = new double[3];
                    gps[0] = coordinate.x;
                    gps[1] = coordinate.y;
                    gps[2] = HEIGHT*j;
                    gpsList.add(gps);
                }
                geo.put("coordinates", gpsList);
                geo.put("name", i > 0 ? j+"-"+i : "area");
                jsonArray.add(geo);
            }
        }
        jsonObject.put("geometries", jsonArray);
        return jsonObject.toJSONString();
    }

    public static String getSql( List<Coordinate> geometry){
        String dataPoint= defaultBuffer.gpsToString(  geometry.toArray(new Coordinate[0]));
        Geometry gps=defaultBuffer.geometry(geometry.toArray(new Coordinate[0]));
        String sql="INSERT INTO `geofence` (" +
                "`service_id`," +
                "`ak`," +
                "`cert_info_id`," +
                "`geofence_name`," +
                "`geofence_shape`," +
                "`coordinates_buffer`," +
                "`end_time`," +
                "`start_time`," +
                "`deleted`," +
                "`group_id`," +
                "`fence_create_type`," +
                "`coordinates`," +
                "`center`," +
                "`radius`," +
                "`poi_id`," +
                "`bounds`," +
                "`buffer`," +
                "`judge_type`," +
                "`status`," +
                "`audit_info`," +
                "`money`," +
                "`audit_time`," +
                "`audit_user_id`," +
                "`create_time`," +
                "`warning_count`," +
                "`geometry_info`," +
                "`adcode`," +
                "`max_geometry` " +
                ")" +
                "VALUES" +
                "(" +
                "" +
                "713," +
                "'FD1635824265606119417A4631F30DB04342A86E12D485369E64NSZETCMM7254'," +
                "254," +
                "'  '," +
                "'polyline'," +
                "'"+dataPoint+"'," +
                "NULL," +
                "NULL," +
                "0," +
                "NULL," +
                "'0'," +
                "'"+dataPoint+"'," +
                "NULL," +
                "NULL," +
                "NULL," +
                "NULL," +
                "0.00," +
                "NULL," +
                "0," +
                "NULL," +
                "0.00," +
                "NULL," +
                "NULL," +
                "'2022-07-16 16:45:46'," +
                "0," +
                "ST_GeomFromText ( '"+gps+"' )," +
                "NULL," +
                "NULL " +
                ");";
        System.out.println(sql);
        return sql;
    }
}
